package com.apache.audit;

import com.apache.api.vo.ParamsVo;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * description:调用日志
 *
 * @author dyh 创建时间：2018年04月04日
 */
public class LogInterceptor implements MethodInterceptor {

    private Logger log = Logger.getLogger("service logs");

    public Object invoke(MethodInvocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = null;
        StringBuilder sb = new StringBuilder("");
        StringBuilder sbStr = new StringBuilder("");
        //监控的类名
        sb.append(invocation.getMethod().getDeclaringClass().getSimpleName());
        //监控的方法名
        sb.append("." + invocation.getMethod().getName());
        Object[] objs = invocation.getArguments();
        try {
            if (null != objs && objs.length > 0) {
                if (objs[0] instanceof ParamsVo) {
                    ParamsVo arguments = (ParamsVo) objs[0];
                    sb.append("->" + arguments.getMethodKey());
                    sbStr.append(";请求参数: " + arguments.toString());
                }
            }
            //这个是我们监控的bean的执行并返回结果
            result = invocation.proceed();
        } catch (Exception e) {
            log.error("执行异常：" + sb.toString() + ",参数:" + getString(objs), e);
            throw e;
        } finally {
            long stopTime = System.currentTimeMillis();
            if (log.isInfoEnabled()) {
                log.info("执行时间:" + (stopTime - startTime) + " ms [" + sb.toString() + "]" + sbStr
                        .toString());
            }
        }
        return result;
    }

    /**
     * 这个类主要是用于输出方法的参数
     *
     * @param objs
     * @return
     */
    @SuppressWarnings("unchecked")
    public String getString(Object[] objs) {
        if (null == objs || objs.length > 0) {
            return "";
        }
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0, len = objs.length; i < len; i++) {
            if (objs[i] instanceof ParamsVo) {
                ParamsVo arguments = (ParamsVo) objs[0];
                stringBuffer.append("ParamsVo参数: " + arguments.toString());
            } else if (objs[i] instanceof String) {
                stringBuffer.append("String类型：" + objs[i].toString());
            } else if (objs[i] instanceof Map) {
                HashMap<String, Object> hashMap = (HashMap<String, Object>) objs[i];
                HashMap<String, Object> map = hashMap;
                HashSet<String> set = (HashSet<String>) map.keySet();
                stringBuffer.append("Map类型");
                for (String str : set) {
                    stringBuffer.append(str + "=" + map.get(str));
                }
            } else if (objs[i] instanceof Integer) {
                stringBuffer.append("整数类型：");
                stringBuffer.append(objs[i].toString());
            } else {
                stringBuffer.append(objs[i].toString());
            }
        }
        return stringBuffer.toString();
    }
}
